<?php

Kurogo::includePackage('db');

class NotesAPIModule extends APIModule {

    protected $id = 'notes';
    protected $vmin = 1;
    protected $vmax = 1;    
    
    protected function initializeForCommand() {
        
        switch($this->command){
            case 'likes':
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                    //CHECK IF USER HAS ALREADY VOTED, IFSO: ON CLICK GET RID OF UPVOTE
                    $postID = $this->getArg('id',0);
                    $db = new db();
                    $sql = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$userID'";
                    $result = $db->query($sql,array());
                    $row = $result->fetch();
                    if($row){
                        $sql = "DELETE FROM likes WHERE postID = '$postID' AND fbID = '$userID'";
                        $db->query($sql);
                        $img = '/modules/posts/images/compliant/nolike.png';
                    }
                    else {
                        $sql = "INSERT into likes (postID, fbID) VALUES ('$postID','$userID')";
                        $db->query($sql);
                        $img = '/modules/posts/images/compliant/like.png';
                    }
                    
                    //COUNT LIKES
                    $sql = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                    $result = $db->query($sql,array());
                    $row = $result->fetch();
                    
                    $response = array('likes' => $row['likes'], 
                                      'img' => $img);
                    $this->setResponse($response);
                    $this->setResponseVersion(1);
                }
                break;
            case 'flags':
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                    //CHECK IF USER HAS ALREADY VOTED, IFSO: ON CLICK GET RID OF UPVOTE
                    $postID = $this->getArg('id',0);
                    $db = new db();
                    $sql = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$userID'";
                    $result = $db->query($sql,array());
                    $row = $result->fetch();
                    if($row){
                        $sql = "DELETE FROM flags WHERE postID = '$postID' AND fbID = '$userID'";
                        $db->query($sql);
                        $img = '/modules/posts/images/compliant/noflag.png';
                    }
                    else {
                        $sql = "INSERT into flags (postID, fbID) VALUES ('$postID','$userID')";
                        $db->query($sql);
                        $img = '/modules/posts/images/compliant/flag.png';
                    }    
                    $response = $img;
                    $this->setResponse($response);
                    $this->setResponseVersion(1);                    
                }
                break;
            case 'reply':
                $session = $this->getSession();
                if($this->isLoggedIn()) {
                    $user = $session->getUser();
                    $userID = $user->getUserID();
                    $postID = $this->getArg('id',0);
                    $message = $this->getArg('message',0);
                    $db = new db();
                    $stripmsg = strip_tags($message);
                    $sql = "INSERT into post (fbID, message) VALUES ('$userID', '$stripmsg')";
                    $db->query($sql);
                    //GET THE POSTID OF THE REPLY
                    
                    $sql = "SELECT postID FROM post WHERE fbID = $userID ORDER BY Date DESC LIMIT 1";
                    $result = $db->query($sql, array('postID'));
                    $row = $result->fetch();
                    $replyID = $row['postID'];
                    $sql = "INSERT into replies (reply_postID, to_postID) VALUES ('$replyID','$postID')";
                    $db->query($sql);
                }
                $this->setResponseVersion(1);
                break;
            case 'addmarkers':
                $db = new db();
                $response = array();
                $sql = "SELECT DISTINCT l.locationID, latitude, longitude 
                        FROM location l, post p 
                        WHERE l.locationID = p.locationID";
                $result = $db->query($sql, array('locationID','latitude','longitude'));
                while($row = $result->fetch())
                {
                    $locID = $row['locationID'];
                    $lat = $row['latitude'];
                    $long = $row['longitude'];
                    $location = array(
                        'locID' => $locID,
                        'lat' => $lat,
                        'lon' => $long
                    );
                    $response[] = $location;
                }
                $this->setResponse($response);
                $this->setResponseVersion(1);
                break;
            case 'getlocations':
                $db = new db();
                $response = array();
                $sql = "SELECT latitude, longitude, name
                        FROM location
                        WHERE acronym<>''";
                $result = $db->query($sql, array('latitude','longitude', 'name'));
                while($row = $result->fetch())
                {
                    $name = $row['name'];
                    $lat = $row['latitude'];
                    $long = $row['longitude'];
                    $location = array(
                        'lat' => $lat,
                        'lon' => $long,
                        'name' => $name
                    );
                    $response[] = $location;
                }
                $this->setResponse($response);
                $this->setResponseVersion(1);
                break;
            case 'getAllLocations':
                //GET ALL LOCATIONS, LAT, LON
                $db = new db();
                $response = array();
                $sql = "SELECT latitude, longitude, locationID
                        FROM location";
                $result = $db->query($sql, array('latitude','longitude', 'locationID'));
                while($row = $result->fetch())
                {
                    $locationID = $row['locationID'];
                    $lat = $row['latitude'];
                    $long = $row['longitude'];
                    $location = array(
                        'lat' => $lat,
                        'lon' => $long,
                        'locID' => $locationID
                    );
                    $response[] = $location;
                }
                $this->setResponse($response);
                $this->setResponseVersion(1); 
        }
    }
}